<?php
class ArticleController extends Controller{
	
	public function filters(){
		return array(
				'accessControl',
		);
	}
	
	public function accessRules(){
		return array(
				array(
						'allow',
						'actions'=>array('list','add', 'edit','delete'),
						'users'=>array('@')
				),
				array(
						'deny',
						'users'=>array('*')
				)
		);
	}
	
	/**
	 * 文章列表
	 * 
	 * @return [type] [description]
	 */
	public function actionList() {
		
		// 通过DAO方式获取文章列表
		$db = Yii::app ()->db;
		
		// 分页
		$countSql = "SELECT COUNT(*) FROM {{article}}";
		$count = $db->createCommand ( $countSql )->queryScalar ();
		$pages = new CPagination ( $count );
		$pages->pageSize = 10;
		$criteria = new CDbCriteria ();
		$pages->applyLimit ( $criteria );
		
		// 获取数据列表
		$offset = $pages->currentPage * $pages->pageSize;
		$limit = $offset . ',' . $pages->pageSize;
		$sql = 'SELECT a.article_id,a.title,a.post_time,c.title as category
				FROM {{article}} a 
				LEFT JOIN {{category}} c ON a.category_id = c.category_id
				ORDER BY article_id DESC
				LIMIT ' . $limit . '
				';
		$list = $db->createCommand ( $sql )->queryAll ();
		
		// 获取属性
		foreach ( $list as &$v ) {
			$sql = "SELECT a.attr_name FROM {{article_attr}} ar
					LEFT JOIN {{attr}} a ON ar.attr_id = a.attr_id
					WHERE ar.article_id = " . $v ['article_id'];
			$v ['attr'] = $db->createCommand ( $sql )->queryAll ();
		}
		
		$this->render ( 'list', array (
				'pages' => $pages,
				'list' => $list 
		) );
	}
	
	/**
	 * 文章添加
	 * 
	 * @return [type] [description]
	 */
	public function actionAdd() {
		$articleModel = new Article ();
		if (isset ( $_POST ['Article'] )) {
			$articleModel->attributes = $_POST ['Article'];
			$articleModel->content = htmlspecialchars ( $articleModel->content );
			$articleModel->post_time = time ();
			$articleModel->user_id = Yii::app()->session['user_id'];
			
			if ($articleModel->save ()) {
				if (isset ( $_POST ['attr_id'] )) {
					$articleId = $articleModel->article_id;
					$articleModel->addAttr ( $articleId, $_POST ['attr_id'] );
				}
				$this->redirect ( array (
						'list' 
				) );
			}
		}
		// 获取栏目下拉列表
		$cateTree = Category::model ()->cateTree (true, false);
		
		// 获取属性
		$attr = Attr::model ()->getAttr ();
		
		Yii::app ()->clientScript->registerScriptFile ( Yii::app ()->request->baseUrl . '/ueditor/ueditor.config.js' );
		Yii::app ()->clientScript->registerScriptFile ( Yii::app ()->request->baseUrl . '/ueditor/ueditor.all.min.js' );
		Yii::app ()->clientScript->registerScriptFile ( Yii::app ()->request->baseUrl . '/ueditor/lang/zh-cn/zh-cn.js' );
		$this->render ( 'add', array (
				'articleModel' => $articleModel,
				'cateTree' => $cateTree,
				'attr' => $attr 
		) );
	}
	
	/**
	 * 文章编辑
	 * 
	 * @return [type] [description]
	 */
	public function actionEdit($aid) {
		$articleModel = Article::model ();
		if (Yii::app ()->request->isPostRequest) {
			$aid = intval ( $aid );
			if ($aid <= 0) {
				Yii::app ()->user->setFlash ( 'error', '参数错误' );
			}
			
			if (isset ( $_POST ['Article'] )) {
				$articleModel->attributes = $_POST ['Article'];
				$articleModel->article_id = $aid;
				$articleModel->content = htmlspecialchars ( $articleModel->content );
				if ($articleModel->save ()) {
					
					// 如果存在attr_id的选择
					if (isset ( $_POST ['attr_id'] ) && count ( $_POST ['attr_id'] ) > 0) {
						$articleModel->addAttr ( $aid, $_POST ['attr_id'] );
					}
					$this->redirect ( array (
							'list' 
					) );
				}
			}
		} else {
			$articleModel = $articleModel->findByPk ( $aid );
			$articleModel->content = htmlspecialchars_decode ( $articleModel->content );
			
			// 获取栏目下拉列表
			$cateTree = Category::model ()->cateTree (true, false);

			// 获取所有属性
			$attr = Attr::model ()->getAttr ();
			
			// 获取该文章已存在属性
			$existAttr = $articleModel->getArticleAttr($aid);

			Yii::app ()->clientScript->registerScriptFile ( Yii::app ()->request->baseUrl . '/ueditor/ueditor.config.js' );
			Yii::app ()->clientScript->registerScriptFile ( Yii::app ()->request->baseUrl . '/ueditor/ueditor.all.min.js' );
			Yii::app ()->clientScript->registerScriptFile ( Yii::app ()->request->baseUrl . '/ueditor/lang/zh-cn/zh-cn.js' );
			$this->render ( 'edit', array (
					'articleModel' => $articleModel,
					'cateTree' => $cateTree,
					'attr' => $attr,
					'existAttr' => $existAttr
			) );
		}
	}
	
	/**
	 * 文章删除
	 */
	public function actionDelete() {
		if (Yii::app ()->request->isAjaxRequest) {
			$aid = Yii::app ()->request->getPost ( 'aid' );
			
			// 删除文章
			$articleRes = Article::model ()->with ( 'attr' )->deleteByPk ( $aid );
			
			// 删除属性
			$sql = 'DELETE FROM {{article_attr}} where article_id = ' . $aid;
			$attrRes = Yii::app ()->db->createCommand ( $sql )->execute ();
			
			// 提示删除成功
			$info = array ();
			$info ['code'] = $articleRes || $attrRes ? 1 : 0;
			echo json_encode ( $info );
		}
	}
}